package com.electricaalberti.controllers.articles;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class ComprobarExistencies {
	
	public void Comprobacio(Connection conexion, int tipusArticla){
		
		Statement st = null;
		ResultSet rs = null;
		
		if(tipusArticla == 1){ //Opcio 1: Mostrar Familias
			
			try{
				
				st = conexion.createStatement();
				rs = st.executeQuery("SELECT * FROM FAMILIA");
				
				System.out.println("===================================================================");
				System.out.println("Listar de Familias");
				System.out.println("-------------------------------------------------------------------");
			
				while (rs.next()){
					System.out.println("\tCodigo= "+rs.getObject("Codi_Familia")+
							",\t Nombre= "+rs.getObject("Nom")
							);
				}
				System.out.println("===================================================================");
			}catch (SQLException e){
				System.out.println("Error al mostrar los datos "+e.toString());
			}
			
		}else if(tipusArticla == 2){ //2: Mostrar Marcas

			try{
				
				st = conexion.createStatement();
				rs = st.executeQuery("SELECT * FROM MARCA");
				
				System.out.println("===================================================================");
				System.out.println("Listar de Marcas");
				System.out.println("-------------------------------------------------------------------");
			
				while (rs.next()){
					System.out.println("\tCodigo= "+rs.getObject("Codi_Marca")+
							",\t Marca= "+rs.getObject("MArca")
							);
				}
				System.out.println("===================================================================");
			}catch (SQLException e){
				System.out.println("Error al mostrar los datos "+e.toString());
			}
		}else if(tipusArticla == 3){ //3: Mostrar Familias i Marcas
			
			try{
				
				st = conexion.createStatement();
				rs = st.executeQuery("SELECT * FROM FAMILIA");
				
				System.out.println("===================================================================");
				System.out.println("Listar de Familias");
				System.out.println("-------------------------------------------------------------------");
			
				while (rs.next()){
					System.out.println("\tCodigo= "+rs.getObject("Codi_Familia")+
							",\t Nombre= "+rs.getObject("Nom")
							);
				}
				
				st = conexion.createStatement();
				rs = st.executeQuery("SELECT * FROM MARCA");
				
				System.out.println("===================================================================");
				System.out.println("Listar de Marcas");
				System.out.println("-------------------------------------------------------------------");
			
				while (rs.next()){
					System.out.println("\tCodigo= "+rs.getObject("Codi_Marca")+
							",\t Marca= "+rs.getObject("MArca")
							);
				}
				System.out.println("===================================================================");
				
			}catch (SQLException e){
				System.out.println("Error al mostrar los datos "+e.toString());
			}

			
		}
	}
	
	/**
	 * 
	 * @param resp recibe SI o NO si en caso de querer o no mostrar las listas
	 */
	public void mostrarListas(Connection conexion, String resp){
		
		Scanner teclado = new Scanner(System.in);

		do{
			System.out.println("Mostrar lista de Familias o Maras? SI o NO");
			System.out.println("===========================================");
			resp = teclado.next();
		}while(resp.equals("Si") || resp.equals("No"));
		
		if(resp.equals("Si")){
			System.out.println("1. Mostrar Marcas");
			System.out.println("2. Mostrar Familias");
			System.out.println("3. Mostrar Familias i Marcas");
			System.out.println("--------------------");
			System.out.println("Selecciona una opcion: ");
			System.out.println("===========================================");
			int tipusArticla = teclado.nextInt();
			Comprobacio(conexion, tipusArticla);
		}
	}

}
